package gui;

import java.awt.Rectangle;
import java.util.ArrayList;
import java.util.List;
import javax.swing.DefaultListModel;
import javax.swing.JPanel;
import javax.swing.JComboBox;
import javax.swing.ListSelectionModel;

import client.Client;
import client.Item;
import client.catalog;
import client.category;
import javax.swing.JList;
import javax.swing.JButton;
import javax.swing.JScrollPane;
import javax.swing.JLabel;

public class AddItemCatalog extends JPanel{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private JComboBox jComboType = null;
	
	private static Client chat;
	private List<category> type = new ArrayList<category>();  //  @jve:decl-index=0:
	private static List<Item> items = new ArrayList<Item>();//  @jve:decl-index=0:
	private List<String> itCat = new ArrayList<String>(); // item that add for the catalog  //  @jve:decl-index=0:
	public String ty;
	private DefaultListModel listModel1=null;
	private DefaultListModel listModel2=null;
	private JList jTreeList = null;

	private JButton addButton = null;

	private JList addList = null;

	public String st;

	private JScrollPane jScrollPane = null;

	private JLabel jItemLabel = null;

	private JLabel jAddLabel = null;

	private JLabel jTypeLabel = null;

	JButton addToCatalogBotton = null;

	private JLabel jDone = null;

	private JButton BackjButton = null;

	/**
	 * 
	 */
	public AddItemCatalog() {
		super();
		initialize();
	}

	private void initialize() {
		jDone = new JLabel();
		jDone.setBounds(new Rectangle(453, 268, 287, 17));
		jDone.setText("");
		jTypeLabel = new JLabel();
		jTypeLabel.setBounds(new Rectangle(98, 47, 77, 17));
		jTypeLabel.setText("choose type");
		jAddLabel = new JLabel();
		jAddLabel.setBounds(new Rectangle(621, 48, 82, 17));
		jAddLabel.setText("items to add");
		jItemLabel = new JLabel();
		jItemLabel.setBounds(new Rectangle(292, 50, 85, 17));
		jItemLabel.setText("choose item");
		this.setSize(1000, 720);
		this.setLayout(null);
		this.add(getJComboType(), null);
		this.add (getJTreeList(), null);
		this.add(getAddButton(), null);
		this.add(getJScrollPane(), null);
		this.add(jItemLabel, null);
		this.add(jAddLabel, null);
		this.add(jTypeLabel, null);
		this.add(getAddToCatalogBotton(), null);
		this.add(jDone, null);
		this.add(getBackjButton(), null);
		
		// TODO Auto-generated method stub
		
	}

	/**
	 * This method initializes jComboType	
	 * 	
	 * @return javax.swing.JComboBox	
	 */
	public static void additems(){
    	catalog cat = new catalog();
		Object b1 = cat.getAllItem();
        items = (List<Item>) b1;
    }
	private JComboBox getJComboType() {
		
		if (jComboType == null) {
			catalog cat = new catalog();
			jComboType = new JComboBox();
			Object b= cat.getAllType();
			type = (List<category>) b;
			jComboType.setBounds(new Rectangle(49, 78, 173, 32));
			for(int i=0; i<type.size();i++){
				jComboType.addItem(type.get(i).getName());
			}	
			jComboType.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					ty = (String)jComboType.getSelectedItem();	
					System.out.println(ty);
					AddItemCatalog.additems();
					listModel1.clear();
					for (int i = 0; i<items.size();i++){
			        	if ((items.get(i).getType().equalsIgnoreCase(ty)) && (!(items.get(i).getCatalogFlag()))){
			        		// show the items in the same type and that not in the catalog
			        		listModel1.addElement(items.get(i).getName());	
			        	}
			        }
					updateUI();
				}
			});	
		}
		return jComboType;
	}
    
	/**
	 * This method initializes jTreeList	
	 * 	
	 * @return javax.swing.JList	
	 */
	private JList getJTreeList() {	
		if (jTreeList == null){
			listModel1 = new DefaultListModel();
			jTreeList = new JList(listModel1);
			jTreeList.setVisibleRowCount(2);
			jTreeList.setBounds(new Rectangle(267, 75, 138, 178));
			jTreeList.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
		} 		
		return jTreeList; 
	}

	/**
	 * This method initializes addButton	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getAddButton() {
		if (addButton == null) {
			addButton = new JButton();
			addButton.setBounds(new Rectangle(468, 98, 85, 31));
			addButton.setText("add");
			addButton.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					Object[] list = jTreeList.getSelectedValues();
					for (int i=0;i<list.length;i++){   //** taking the selected values and add to the addList 
						int flag = 0; // check if value already in the addList
						for (int j=0; j<addList.getModel().getSize();j++){
							if(list[i].equals(addList.getModel().getElementAt(j)))
								flag = 1;
						}
						if (flag ==0) listModel2.addElement(list[i]);	
				}
			}});
		}
		return addButton;
	}

	/**
	 * This method initializes addList	
	 * 	
	 * @return javax.swing.JList	
	 */
	private JList getAddList() {
		if (addList == null) {
			listModel2 = new DefaultListModel();
			addList = new JList(listModel2);
			addList.setVisibleRowCount(4);

			
		}
		return addList;
	}

	/**
	 * This method initializes jScrollPane	
	 * 	
	 * @return javax.swing.JScrollPane	
	 */
	private JScrollPane getJScrollPane() {
		if (jScrollPane == null) {
			jScrollPane = new JScrollPane(addList);
			jScrollPane.setBounds(new Rectangle(614, 84, 103, 98));
			jScrollPane.setViewportView(getAddList());
		}
		return jScrollPane;
	}

	/**
	 * This method initializes addToCatalogBotton add the items that chosen to the catalog
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getAddToCatalogBotton() {
		if (addToCatalogBotton == null) {
			addToCatalogBotton = new JButton();
			addToCatalogBotton.setBounds(new Rectangle(602, 210, 114, 22));
			addToCatalogBotton.setText("add to catalog");
			addToCatalogBotton.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					for (int i=0; i<addList.getModel().getSize();i++){
						itCat.add(addList.getModel().getElementAt(i).toString());
					}
					catalog cat = new catalog(itCat); //the name of the items that add to the catalog
					Boolean done = false;
					Object b2 = cat.addCatalog();
					done = (Boolean)b2;
					if (done) jDone.setText("item add to catalog");
				}
			});
		}
		return addToCatalogBotton;
	}

	/**
	 * This method initializes BackjButton	
	 * 	
	 * @return javax.swing.JButton	
	 */
	JButton getBackjButton() {
		if (BackjButton == null) {
			BackjButton = new JButton();
			BackjButton.setBounds(new Rectangle(597, 312, 115, 56));
			BackjButton.setText("BACK");
		}
		return BackjButton;
	}
	
	
	
}  //  @jve:decl-index=0:visual-constraint="12,15"